package com.lanou.dllo.shise.com.lanou.dllo.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;

import com.lanou.dllo.shise.R;

public class SplashActivity extends AppCompatActivity {
    private String email,password;
    private boolean isAutoLogin;
    private SharedPreferences sp;
    private Handler handler;//定义一个handler对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //从缓存文件中取用户名,密码,以及自动登录标识
        sp = getSharedPreferences("lanou3g", Context.MODE_PRIVATE);
        email = sp.getString("email", "");
        password = sp.getString("password", "");
        isAutoLogin = sp.getBoolean("isAutoLoginCb", false);

        //初始化handler 并接受消息接收回调
        handler = new Handler(new Handler.Callback(){
            @Override
            public boolean handleMessage(Message msg) {
                //页面跳转

                Intent intent = new Intent();
                if (isAutoLogin && email != null && email.length() > 0 && password != null && password.length() > 0) {
                    intent.setClass(SplashActivity.this, MainActivity.class);

                    intent.putExtra("email", email);
                    intent.putExtra("password", password);
                } else
                    intent.setClass(SplashActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();//关闭当前页面
                    return false;
                }

            });
        handler.sendEmptyMessageDelayed(100,2000);
        }
    }

